package cn.ekuma.epos.productsfinishing;

import java.util.Properties;

import com.openbravo.bean.ProductPrice;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import cn.ekuma.data.ui.swing.AbstractChildDAOJEditor;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import javax.swing.SwingConstants;

public class ProductPriceEditor extends AbstractChildDAOJEditor<ProductPrice,String> {
	private JTextField txtGroupName;
	private JTextField txtSalePrice;
	private JTextField txtCustomerPrice;

	public ProductPriceEditor(DirtyManager dirty) {
		super(dirty);
		setPreferredSize(new Dimension(351, 117));
		initComponents();
		txtGroupName.getDocument().addDocumentListener(dirty);
		txtSalePrice.getDocument().addDocumentListener(dirty);
		txtCustomerPrice.getDocument().addDocumentListener(dirty);
	}
	private void initComponents() {
		setLayout(null);
		
		JLabel label = new JLabel();
		label.setBounds(48, 10, 138, 21);
		label.setText(AppLocal.getIntString("label.ProductPrice.groupName"));
		add(label);
		
		txtGroupName = new JTextField();
		txtGroupName.setHorizontalAlignment(SwingConstants.CENTER);
		txtGroupName.setBounds(196, 10, 109, 21);
		add(txtGroupName);
		
		JLabel label_1 = new JLabel();
		label_1.setText(AppLocal.getIntString("label.prodpricesell"));
		label_1.setBounds(48, 41, 150, 21);
		add(label_1);
		
		txtSalePrice = new JTextField();
		txtSalePrice.setHorizontalAlignment(SwingConstants.RIGHT);
		txtSalePrice.setBounds(198, 41, 109, 21);
		add(txtSalePrice);
		
		JLabel label_2 = new JLabel();
		label_2.setText(AppLocal.getIntString("label.baseCustomerPrice"));
		label_2.setBounds(48, 76, 150, 21);
		add(label_2);
		
		txtCustomerPrice = new JTextField();
		txtCustomerPrice.setHorizontalAlignment(SwingConstants.RIGHT);
		txtCustomerPrice.setBounds(198, 76, 109, 21);
		add(txtCustomerPrice);
	}

	@Override
	public void refresh() {
		
	}

	@Override
	public String getTabName() {
		return AppLocal.getIntString("label.ProductPrice");
	}

	@Override
	public ProductPrice createNew(Properties prop) {
		ProductPrice obj=new ProductPrice();
		if(parentNode!=null)
			obj.setProdcutID(parentNode.getParentKey().getKey());
		return obj;
	}

	@Override
	public void rebuild(ProductPrice obj) throws BasicException {
		obj.setGroupName(txtGroupName.getText());
		obj.setM_dPriceSell((Double) Formats.CURRENCY.parseValue(txtSalePrice.getText()));
		obj.setM_dCustomerPrice((Double) Formats.CURRENCY.parseValue(txtCustomerPrice.getText(), 0.0));
	}

	@Override
	public void updateUI(boolean editAble, int state) {
		txtSalePrice.setText(Formats.CURRENCY.formatValue(obj.getM_dPriceSell()));
		txtCustomerPrice.setText(Formats.CURRENCY.formatValue(obj.getM_dCustomerPrice()));
		txtGroupName.setText(obj.getGroupName());
		txtCustomerPrice.setEditable(editAble);
		txtSalePrice.setEditable(editAble);
		txtGroupName.setEditable(editAble);
	}

	@Override
	public Class getEditClass() {
		return ProductPrice.class;
	}
}
